package com.example.demo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class StuContentProvider extends ContentProvider {
    private StuOpenHelper stuOpenHelper;
    private static final String AUTOHORITY ="com.gdcp.student";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTOHORITY,"student",1);
        uriMatcher.addURI(AUTOHORITY,"score",2);
    }

    @Override
    public boolean onCreate() {
        stuOpenHelper = new StuOpenHelper(getContext(),"stu.db",null,1);
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        SQLiteDatabase db=stuOpenHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                Cursor cursor1 = db.query("student",projection,selection,selectionArgs,null,null,sortOrder);
                return cursor1;
            case 2:
                Cursor cursor2 = db.query("score",projection,selection,selectionArgs,null,null,sortOrder);
                return cursor2;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        SQLiteDatabase db=stuOpenHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                db.insert("student",null,values);
                break;
            case 2:
                db.insert("score",null,values);
                break;
        }
        db.close();
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = stuOpenHelper.getWritableDatabase();
        switch(uriMatcher.match(uri)){
            case 1:
                int i1 = db.delete("student",selection,selectionArgs);
                db.close();
                return i1;
            case 2:
                int i2 = db.delete("score",selection,selectionArgs);
                db.close();
                return i2;
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = stuOpenHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                int i1 = db.update("student",values,selection,selectionArgs);
                db.close();
                return i1;
            case 2:
                int i2 = db.update("score",values,selection,selectionArgs);
                db.close();
                return i2;
        }

        return 0;
    }
}
